<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title>签名</title>
		<style type="text/css">
			* {
				padding: 0;
				margin: 0;
			}
			
			.container {
				width: 100vw;
				height: 100vh;
				display: flex;
				align-items: center;
				justify-content: center;
			}
			
			canvas {
				width: 500px;
				height: 500px;
				border: 2px solid blue;
			}
		</style>
	</head>

	<body>
		<div class="container">
			<canvas id="canvas" width="500" height="500"></canvas>
		</div>
	</body>
	<script type="text/javascript">
		window.onload = function() {
			let oCanvas = document.getElementById('canvas');
			console.dir(oCanvas);
			let oPen = oCanvas.getContext("2d");
			console.dir(oPen);
			oPen.lineWidth=5;
			
			document.onmousedown=function(e){
				x=+(e.pageX-oCanvas.offsetLeft);
				y=+(e.pageY-oCanvas.offsetTop);
				oPen.moveTo(x,y);
				s=1;/*鼠标是否已经按下*/
				document.onmousemove=function(e){
					x=(e.pageX-oCanvas.offsetLeft);
					y=(e.pageY-oCanvas.offsetTop);
					if(s==1){
						oPen.lineJoin="round";
						oPen.lineTo(x,y);
						oPen.stroke();
					}								
				}
				document.onmouseup=function(e){
					x1=(e.pageX-oCanvas.offsetLeft);
					y1=(e.pageY-oCanvas.offsetTop);
					s=0;
				}
			}
		}
	</script>
	</body>

</html>